/*****************************************************************************
 *  This file is part of the OpenDomo project.
 *  Copyright(C) 2011 OpenDomo Services SL
 *  
 *  Daniel Lerch Hostalot <dlerch@opendomo.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *****************************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <perceiving/vision/jpeg.h>
#include <perceiving/vision/point.h>
#include <perceiving/vision/image.h>
#include <perceiving/vision/edge.h>
#include <perceiving/vision/thresholding.h>
#include <coelacanth/vector.h>


int human_recognition(image_t *img)
{
   printf("width: %ld\n", img->width);
   printf("height: %ld\n", img->height);
   printf("components: %ld\n", img->components);

   image_gen_grayscale(img);

   point_t p;
  
   /*
   vector(vpoint, vpoint_size, point_t);

   p.x = 10;
   p.y = 10;
   vector_add(vpoint, vpoint_size, point_t, p);

   p.x = 100;
   p.y = 15;
   vector_add(vpoint, vpoint_size, point_t, p);

   p.x = 150;
   p.y = 150;
   vector_add(vpoint, vpoint_size, point_t, p);

   p.x = 15;
   p.y = 150;
   vector_add(vpoint, vpoint_size, point_t, p);


   image_draw_shape(img, vpoint, vpoint_size);
   */

   p.x = 100;
   p.y= 100;
   image_draw_circle(img, &p, 50);

   /*
   int x, y;
   for(y=0; y<img->height; y++)
   {
      for(x=0; x<img->width; x++)
      {
         if(image_get(img, x, y)==0)
         {
            image_set_R(img, x, y, 255);
            image_set_G(img, x, y, 0);
            image_set_B(img, x, y, 0);
            break;
         }
      }
   }
   */

   // edge_detection(img, CANNY_FILTER);
   //thresholding(img, ITERATIVE_METHOD);

   return 0;
}



int main(int argc, char*argv[])
{
   /* parse input */
   if(argc!=3)
   {
      puts("Usage: %s <background jpeg> <motion capture jpeg>\n\n");
      return 0;
   }

   /* read input images */
   image_t *img_bg = jpeg_read(argv[1]);
   image_t *img_motion = jpeg_read(argv[2]);

   /* background extraction */
   image_t *img_shape = image_diff(img_bg, img_motion);

   /* edge detection */
   //edge_detection(img_shape, CANNY_FILTER);

   /* human recognition */
   if(human_recognition(img_shape))
      puts("Human Recognition");
   else
      puts("No Human Detection");

   /* write output */
   jpeg_write(img_shape, 100, "out.jpg");

   /* free resources */
   free(img_bg);
   free(img_motion);
   free(img_shape);

   return 0;
}



